
<?php $title = "Role" ?>
<?php require_once("header.php"); ?>
<?php
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $role = ($_POST["role"] == "null" ? die("INTERNAL CODE ERROR 'ROLE'") : $_POST["role"]);
        $type = ($_POST["type"] == "null" ? die("INTERNAL CODE ERROR 'TYPE'") : $_POST["type"]);
        $person = ($_POST["person"] == "null" ? $type : $_POST["person"]);
        if($type == "apply"){
            //create new apply
            //check for positions
            if(Role::isAvailable($role))
            {
                Role::applyPos($role,$personId);
                $page->addMessage("Application Submitted for the Role!<br/>");
            }
            else
            {
                $page->addMessage("No positions Available!<br/>");
            }
        }
        else 
        {
            Role::withdraw($role,$personId);
            $page->addMessage("Withdrawal from the role successful<br/>");
        }
    }
    $action = "viewAll";

    if (isset($_GET["roleId"])) {
        $view = new RoleController(Core::sanitize($_REQUEST["roleId"]));

        if ($view->roleExists()) {
            $action = "specificView";
            }
?>
<div class="centerContent">
    <label class="pageMessage"><?php ($loggedIn ? $page->printMessages() : false); ?></label>
    <?php
        if ($action == "viewAll") {
            $data = RoleController::getAllRoles("0000-00-00");
            for ($i = 0; $i < count($data); $i++) {
                $role = new RoleController($data[$i]['roleId']);
                ?>
                <div class="list" onclick='UTSDK.redirect("viewRole.php?roleId=<?= $data[$i]['roleId'] ?>");'>
                    <h2>Role: <?= $role->getTitle() ?></h2>
                    <table class="listTable">
                        <tr><td colspan="2">
                                <h4  class="wordWrap"><i><?= $role->getDescription() ?></i></h4>
                            </td></tr>
                        <tr><td><b>Date : </b></td><td><?= $role->getStartAndEndDates() ?></td></tr>
                        <tr><td><b>Positions : </b></td><td><?= $role->getPositions() ?></td></tr>
                        <tr><td><b>Event : </b></td>
                            <td><a href="viewEvent.php?eventId=<?= $role->getEventId() ?>">View <?= $role->getEventName() ?></a></td></tr>
                        <tr>
                        <tr><td><b>Activity : </b></td>
                            <td><a href="viewActivity.php?activityId=<?= $role->getActivityId() ?>">View <?= $role->getActivityName() ?></a></td></tr>
                        <tr>
                            <td colspan="2">
                                <a href="viewRole.php?roleId=<?= $data[$i]['roleId'] ?>"><i>View Role</i></a>
                            </td>
                        </tr>
                    </table>
                </div>
                <?php
            }
        } else if ($action == "specificView") {
            ?>
            <button onclick="UTSDK.redirect('viewActivity.php?activityId=<?= $view->getActivityId() ?>')">Go to Activity</button>
            <table>
                <tr><td colspan="2"><h1><?= $view->getTitle(); ?></h1></td></tr>
                <tr><td colspan="2" class="wordWrap"><i><?= $view->getDescription(); ?></i></td></tr>
                <tr><td><b>Start and End Date : </b></td><td><?= $view->getStartAndEndDates() ?></td></tr>
                <tr><td><b>Start and End Time : </b></td><td><?= $view->getStartAndEndTimes() ?></td></tr>
                <tr><td><b>Positions : </b></td><td><?= $view->getPositions() ?></td></tr>
                <tr><td><b>Skills Required : </b></td><td><?= $view->getSkills() ?></td></tr>
                <tr><td><b>Paid : </b></td><td id="payRate"><?= $view->getPaid() ?></td></tr>
                <tr><td><b>Event : </b></td>
                    <td><a href="viewEvent.php?eventId=<?= $view->getEventId() ?>">View <?= $view->getEventName() ?></a></td></tr>
                <tr>
                <tr><td><b>Activity : </b></td>
                    <td><a href="viewActivity.php?activityId=<?= $view->getActivityId() ?>">View <?= $view->getActivityName() ?></a></td>
                </tr>

                <tr><td></td><td><input id="applybutton" type="button" onclick="apply(event)" 
                                        value="Apply" style="width: 20em;"/></td></tr>
            </table>
            <script>
                window.addEventListener("load", function() {
                    var payRate = UTSDK.$("payRate");
                    payRate.textContent = UTSDK.toDollarFormat(payRate.textContent);
                    var applybtn = UTSDK.$("applybutton");
                    var loggedIn = <?= $loggedIn + "" ?>;
                    if (loggedIn === "0" || loggedIn === 0) {
                        applybtn.style.display = "none";
                        applybtn.parentElement.innerHTML = "Login to apply";
                    }
                    else {
                        var json = {
                            appliedPerson: <?=(isset($personId) ? $personId : 0) + "" ?>,
                            appliedRole: <?= (isset($_REQUEST["roleId"]) ? $_REQUEST["roleId"] : 0) ?>
                        };
                        UTSDK.AJAX.get("update.php", json, function(responce) {
                            responce = JSON.parse(responce);
                            if (responce["accepted"] === "pending")
                                applybtn.value = "Withdraw Application";
                            else if (responce["accepted"] === "accepted")
                                applybtn.parentElement.innerHTML = "Contact UTS Event Management to withdraw application";
                            else if(responce["accepted"] === "rejected") {
                                applybtn.parentElement.innerHTML = "Applicantion rejected";
                            }
                        }, true);
                    }
                });

                function apply(evt) {
                    var form = document.createElement("FORM");
                    form.method = "POST";
                    form.action = UTSDK.getCurrentUrl();
                    var name = document.createElement("INPUT");
                    name.name = "formSource";
                    name.value = "rApply";
                    form.action = UTSDK.getCurrentUrl();
                    var person = document.createElement("INPUT");
                    person.name = "person";
                    person.value = <?=(isset($personId) ? $personId : 0).""?>;
                    var role = document.createElement("INPUT");
                    role.name = "role";
                    role.value = "<?= $_REQUEST["roleId"] ?>";
                    var applicationType = document.createElement("INPUT");
                    applicationType.name = "type";
                    if (evt.target.value === "Apply")
                        applicationType.value = "apply";
                    else
                        applicationType.value = "withdraw";
                    form.appendChild(person);
                    form.appendChild(role);
                    form.appendChild(applicationType);
                    form.style.display ="none";
                    window.document.body.appendChild(form);
                    form.submit();
                }
            </script>
            <?php
        } else if ($action == "dateView") {
            $data = RoleController::getAllRolesOn($date);
            for ($i = 0; $i < count($data); $i++) {
                $role = new RoleController($data[$i]['roleId']);
                ?>
                <div class="list" onclick='UTSDK.redirect("viewRole.php?roleId=<?= $data[$i]['roleId'] ?>");'>
                    <h2>Role: <?= $role->getTitle() ?></h2>
                    <table class="listTable">
                        <tr><td colspan="2">
                                <h4  class="wordWrap"><i><?= $role->getDescription() ?></i></h4>
                            </td></tr>
                        <tr><td><b>Date : </b></td><td><?= $role->getStartAndEndDates() ?></td></tr>
                        <tr><td><b>Positions : </b></td><td><?= $role->getPositions() ?></td></tr>
                        <tr><td><b>Event : </b></td>
                            <td><a href="viewEvent.php?eventId=<?= $role->getEventId() ?>">View <?= $role->getEventName() ?></a></td></tr>
                        <tr>
                        <tr><td><b>Activity : </b></td>
                            <td><a href="viewActivity.php?activityId=<?= $role->getActivityId() ?>">View <?= $role->getActivityName() ?></a></td></tr>
                        <tr>
                            <td colspan="2">
                                <a href="viewRole.php?roleId=<?= $data[$i]['roleId'] ?>"><i>View Role</i></a>
                            </td>
                        </tr>
                    </table>
                </div>
                <?php
            }
        }
    }
    ?>
</div>
<?php
    require_once("footer.php");

    ?>
    